package timeline1;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * First working Draft of a proof of concept for a time-line for animation
 * */
public class Main extends JPanel implements MouseListener
, MouseMotionListener, ComponentListener, KeyListener,Runnable {

JFrame frame;
Thread thread;
UserPoint head,tail;

public Main(){frame=new JFrame(
		"Space Invaders from http://kgdug.org, QuickAndDirtyImplementation by MohamadJB");
	frame.setContentPane(this);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setBounds(10,10, 800, 600);

	addMouseListener(this);
	addMouseMotionListener(this);
	addKeyListener(this);
	addComponentListener(this);
	frame.setVisible(true);
	setDim(getSize());
	thread=new Thread(this);
	thread.start();}

 public void paint(Graphics g)
 {Dimension d=getDim();
	g.clearRect(0,0, d.width ,d.height);
 //g.drawString("kills:"+kills,10,10);
 //g.drawString("lives:"+lives,100,10);
 //g.drawString("level:"+level,200,10);
 //	invaders.paint(g);
	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.paint(g);}
	catch(Exception ex){ex.printStackTrace();}}

 public void run()
 {Thread current=Thread.currentThread();
	while(thread==current)
	try{Dimension d=getDim();
	//	invaders.incr(d);
	//	bullets.incr(d);
		frame.repaint();
		thread.sleep(100);
	}catch(Exception ex){ex.printStackTrace();}}

	static Dimension _dim;
	static Dimension getDim(){return _dim;}
	static void setDim(Dimension p)
	{System.out.println("setDim:"+p);
		_dim=p;}

 @Override	public void mouseMoved(MouseEvent e)
 {	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.mouseMoved(e);}
	catch(Exception ex){ex.printStackTrace();}}


 @Override	public void mouseReleased(MouseEvent e)
 {	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.mouseReleased(e);}
	catch(Exception ex){ex.printStackTrace();}}

	@Override	public void mouseDragged(MouseEvent e)
	{	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.mouseDragged(e);}
	catch(Exception ex){ex.printStackTrace();}}

	@Override	public void mouseClicked(MouseEvent e){}

 @Override	public void mousePressed(MouseEvent e)
 {	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.mouseMoved(e);}
	catch(Exception ex){ex.printStackTrace();}}

 @Override	public void mouseEntered(MouseEvent e)
 {	UserPoint n=head,nn;
	while(n!=null)try{nn=n;n=n.next;nn.mouseMoved(e);}
	catch(Exception ex){ex.printStackTrace();}}

 @Override	public void mouseExited(MouseEvent e) {}

 @Override	public void keyReleased(KeyEvent e)
 {//if fire button pressed
	}

	@Override	public void keyTyped(KeyEvent e) {}
	@Override	public void keyPressed(KeyEvent e) {}
	@Override	public void componentResized(ComponentEvent e){}
	@Override	public void componentMoved(ComponentEvent e) {}
	@Override	public void componentShown(ComponentEvent e) {}
	@Override	public void componentHidden(ComponentEvent e){}

	/** entry point for main program.	 */
	public static void main(String[] args) {new Main();}
////////////////////////////////////////////////////////////////
class UserPoint
{int x,y;float r=5;

 /**for display / active */
 UserPoint next,prev;

 UserPoint link(UserPoint p)
 {	if(head==null)//p==null);
		head=tail=this;
	else{if(p==head){p.prev=this;next=p;prev=null;head=this;}
	 else{prev=p.prev;next=p;prev.next=p.prev=this;}
	}return this;}

 UserPoint linkAfter(UserPoint p)
 {	if(head==null)//p==null);
		head=tail=this;
	else{if(p==tail){p.next=this;prev=p;next=null;tail=this;}
	 else{next=p.next;prev=p;p.next=next.prev=this;}
	}return this;}

 UserPoint unlink()
 {	if(prev!=null)prev.next=next;
	if(next!=null)next.prev=prev;
	if(head==this)head=next;
	if(tail==this)tail=prev;
	next=prev=null;return this;}

 public void paint(Graphics g)
 {int v=(int)(r/2),z=(int)r;
	g.drawOval(x-v, y-v, z, z);}


	double dist(int x,int y){double a=this.x-x,b=this.y-y;return Math.sqrt(a*a+b*b);}
	boolean isOver(int x,int y){return dist(x,y)<=r;}
	public void mouseMoved(MouseEvent e){}
	public void mouseDragged(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseClicked(MouseEvent e) {}
	public void mousePressed(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}

}//UserPoint
////////////////////////////////////////////////////////////////
class Box extends UserPoint
{int w,h;
 HashMap<String,UserPoint> points=new HashMap<String,UserPoint>();

 

}//Box
////////////////////////////////////////////////////////////////
	class TimeLine
	{Rectangle rect=new Rectangle(0,0,100,100);

	public void paint(Graphics g)
	{
		
	}

	Tracker newTracker()
	{return new Tracker();}

	class Tracker
	{
		TimeLine getTimeLine(){return TimeLine.this;}
	}

}//TimeLine

////////////////////////////////////////////////////////////////

}//Main
